import 'package:flutter/material.dart';
import 'package:flutter_deer/login/login_router.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/styles.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/setting/iview/account_logout.dart';
import 'package:flutter_deer/setting/presenter/account_logout_presenter.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/base_dialog.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:sp_util/sp_util.dart';

class ExitDialog extends StatefulWidget {

  const ExitDialog({
    super.key,
  });

  @override
  _ExitDialog createState() => _ExitDialog();
  
}

class _ExitDialog extends State<ExitDialog> with BasePageMixin<ExitDialog, PowerPresenter> implements AccountLogoutIMvpView {

  late AccountLogoutPresenter accountLogoutPresenter;

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    accountLogoutPresenter = new AccountLogoutPresenter();
    powerPresenter.requestPresenter([accountLogoutPresenter]);
    return powerPresenter;
  }

  @override
  void setLogoutResult(bool? result){
    if(result==true){
      SpUtil.remove(Constant.accessToken);
      SpUtil.remove(Constant.refreshToken);
      SpUtil.remove(Constant.tokenType);
      SpUtil.remove(Constant.phone);
      NavigatorUtils.push(context, LoginRouter.loginPage, clearStack: true);
    }else{
      showToast('${DeerLocalizations.of(context)!.exitFail.nullSafe}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return BaseDialog(
      title: '${DeerLocalizations.of(context)!.prompt.nullSafe}',
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
        child: Text('${DeerLocalizations.of(context)!.wantToLogOut.nullSafe}', style: TextStyles.textSize16),
      ),
      onPressed: () {
        accountLogoutPresenter.logout();
      },
    );
  }
}
